package com.example.studyonlineapplication.fragments;

import android.content.ContentProviderOperation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.studyonlineapplication.R;
import com.example.studyonlineapplication.domain.po.Result;
import com.example.studyonlineapplication.utils.MyToast;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NoteEntityFragment extends Fragment {
    ImageView avator;
    TextView textAbove,textBottom,textBody,subject;
    ImageButton delTextBtn;
    int noteId;
    private final OkHttpClient client = new OkHttpClient();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.note_entity,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        avator = view.findViewById(R.id.avator);
        textAbove = view.findViewById(R.id.textAbove);
        textBottom = view.findViewById(R.id.textBottom);
        textBody = view.findViewById(R.id.textBody);
        subject = view.findViewById(R.id.subject);
        delTextBtn = view.findViewById(R.id.delTextBtn);

        textAbove.setText(getArguments().getString("username"));
        textBottom.setText(getArguments().getString("updatetime"));
        textBody .setText(getArguments().getString("noteText"));
        subject.setText(getArguments().getString("courseName"));
        noteId = getArguments().getInt("noteId");

        delTextBtn.setOnClickListener(v->{
            String url = "http://10.0.2.2:8080/note/delNote?noteId=" + noteId;
            Request request = new Request.Builder()
                    .url(url)
                    .delete() // DELETE 请求
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    String json = response.body().string();
                    System.out.println(json);
                    Gson gson = new Gson();
                    Result<String> data = gson.fromJson(json, Result.class);
                    getActivity().runOnUiThread(()->{
                        if (data.getCode()==1){
                            MyToast.message("删除失败2",getContext());
                        }else {
                            MyToast.message("删除成功",getContext());
                        }
                    });
                }

                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    getActivity().runOnUiThread(()->{
                        MyToast.message("删除失败",getContext());
                    });
                }
            });
            
        });
    }
}
